package com.example.tang.zhayan.utils;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.tang.zhayan.R;
import com.example.tang.zhayan.ui.UserLoginActivity;


import java.util.List;

/**
 * Created by tang on 2015/12/14.
 * <p/>
 * 主界面RadioGroup的点击切换Fragment工具类
 */
public class FragmentUtils {

    private LinearLayout llImage;
    private LinearLayout llText;
    private LinearLayout llContainer;
    private List<Fragment> list;
    private FragmentManager manager;
    //fragment容器id
    private int containerId;
    private Context context;


    public FragmentUtils(FragmentManager manager, List<Fragment> list) {
        this.list = list;
        this.manager = manager;

    }


    public FragmentUtils(LinearLayout llContainer, LinearLayout llImage, LinearLayout llText, List<Fragment> list, FragmentManager manager, int containerId, Context context) {
        this.list = list;
        this.manager = manager;
        this.containerId = containerId;
        this.context = context;
        this.llImage = llImage;
        this.llText = llText;
        this.llContainer = llContainer;
        llImage.getChildAt(0).setEnabled(false);
        llText.getChildAt(0).setEnabled(false);
        linearLayoutClick();
    }


    /**
     * 显示Fragment
     *
     * @param index
     */
    public void showFragment(int index) {
        FragmentTransaction transaction = manager.beginTransaction();
        Fragment fragment = null;

        for (int i = 0; i < list.size(); i++) {
            fragment = list.get(i);
            if (!fragment.isAdded()) {
                transaction.add(containerId, fragment);
            }
        }
        for (int i = 0; i < list.size(); i++) {
            fragment = list.get(i);
            if (i == index) {
                transaction.show(fragment);
            } else {
                transaction.hide(fragment);
            }
        }
        transaction.commit();


    }

    public void setEnable(int positoin, LinearLayout v) {
        for (int i = 0; i < v.getChildCount(); i++) {
            v.getChildAt(i).setEnabled(true);
        }
        v.getChildAt(positoin).setEnabled(false);
    }


    /**
     * 主界面菜单的点击事件
     */
    public void linearLayoutClick() {
        for (int i = 0; i < llImage.getChildCount(); i++) {
            final ImageView imageView = (ImageView) llImage.getChildAt(i);
            final TextView textView = (TextView) llText.getChildAt(i);
            if (i < llImage.getChildCount() - 1) {
                imageView.setTag(i);
                imageView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int index = (int) imageView.getTag();
                        showFragment(index);
                        setEnable(index, llImage);
                        setEnable(index, llText);
                        if (index == 0) {
                            llContainer.setBackgroundResource(R.color.black);
                        } else {
                            llContainer.setBackgroundResource(R.color.whitesmoke);
                        }
                    }
                });
                textView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int index = (int) imageView.getTag();
                        showFragment(index);
                        setEnable(index, llImage);
                        setEnable(index, llText);
                        if (index == 0) {
                            llContainer.setBackgroundResource(R.color.black);
                        } else {
                            llContainer.setBackgroundResource(R.color.whitesmoke);
                        }
                    }
                });

            }
        }

    }
}



